﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using com.hyrentals.common;


namespace com.hyrentals.webcommon.extensions
{
    public static class ValidationExtensions
    {
        public static string GetValidationMessage(this HtmlHelper htmlHelper, string modelName)
        {
            ModelStateDictionary modelStateDic = htmlHelper.ViewData.ModelState;
            foreach (KeyValuePair<string, ModelState> modelEntry in modelStateDic)
            {
                if (modelEntry.Key == modelName)
                {
                    if (modelEntry.Value.Errors.Count == 0)
                        return null;

                    return modelEntry.Value.Errors[0].ErrorMessage;
                }
            }
            return null;
        }

        public static string GetValidationMessage(this ModelStateDictionary modelStateDic, string modelName)
        {
            foreach (KeyValuePair<string, ModelState> modelEntry in modelStateDic)
            {
                if (modelEntry.Key == modelName)
                {
                    if (modelEntry.Value.Errors.Count == 0)
                        return null;

                    return modelEntry.Value.Errors[0].ErrorMessage;
                }
            }
            return null;
        }

        public static List<object> GenerateErrorList(this ModelStateDictionary modelStateDic)
        {
            List<object> errorList = new List<object>();
            foreach (KeyValuePair<string, ModelState> modelEntry in modelStateDic)
            {
                if (modelEntry.Value.Errors.Count > 0)
                {
                    errorList.Add(new { id = modelEntry.Key, msg = modelEntry.Value.Errors[0].ErrorMessage });
                }
            }
            return errorList;
        }

        public static void Required(this ModelStateDictionary modelStateDic, string key, string value, string text)
        {
            ValidationResult result = ValidationLogic.Required(value, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Regex(this ModelStateDictionary modelStateDic, string key, string value, string pattern, string text, RegexOptions regexOptions = RegexOptions.IgnoreCase)
        {
            ValidationResult result = ValidationLogic.Regex(value, pattern, text, regexOptions);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void ASCIIPrintChar(this ModelStateDictionary modelStateDic, string key, string value, string text)
        {
            ValidationResult result = ValidationLogic.ASCIIPrintChar(value, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Date(this ModelStateDictionary modelStateDic, string key, string value, string text)
        {
            ValidationResult result = ValidationLogic.Date(value, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Year(this ModelStateDictionary modelStateDic, string key, string value, string text)
        {
            ValidationResult result = ValidationLogic.Year(value, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Zip(this ModelStateDictionary modelStateDic, string key, string value, string text)
        {
            ValidationResult result = ValidationLogic.Zip(value, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Email(this ModelStateDictionary modelStateDic, string key, string value, string text)
        {
            ValidationResult result = ValidationLogic.Email(value, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Url(this ModelStateDictionary modelStateDic, string key, string value, string text)
        {
            ValidationResult result = ValidationLogic.Url(value, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Mobile(this ModelStateDictionary modelStateDic, string key, string value, string text)
        {
            ValidationResult result = ValidationLogic.Mobile(value, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void TelPhone(this ModelStateDictionary modelStateDic, string key, string value, string text)
        {
            ValidationResult result = ValidationLogic.TelPhone(value, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void TelOrMobilePhone(this ModelStateDictionary modelStateDic, string key, string value, string text)
        {
            ValidationResult result = ValidationLogic.TelOrMobilePhone(value, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Length(this ModelStateDictionary modelStateDic, string key, string value, string text, int minLen, int maxLen, bool isRealLength = false)
        {
            ValidationResult result = ValidationLogic.Length(value, text, minLen, maxLen, isRealLength);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void MaxLength(this ModelStateDictionary modelStateDic, string key, string value, string text, int maxLen, bool isRealLength = false)
        {
            ValidationResult result = ValidationLogic.MaxLength(value, text, maxLen, isRealLength);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void MinLength(this ModelStateDictionary modelStateDic, string key, string value, string text, int minLen, bool isRealLength = false)
        {
            ValidationResult result = ValidationLogic.MinLength(value, text, minLen, isRealLength);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void NoSpace(this ModelStateDictionary modelStateDic, string key, string value, string text)
        {
            ValidationResult result = ValidationLogic.NoSpace(value, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void EqualTo(this ModelStateDictionary modelStateDic, string key, string value1, string value2, string text)
        {
            ValidationResult result = ValidationLogic.EqualTo(value1, value2, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Latlng(this ModelStateDictionary modelStateDic, string key, double latitude, double longitude, string text)
        {
            ValidationResult result = ValidationLogic.Latlng(latitude, longitude, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Range(this ModelStateDictionary modelStateDic, string key, double value, double min, double max, string text)
        {
            ValidationResult result = ValidationLogic.Range(value, min, max, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Min(this ModelStateDictionary modelStateDic, string key, double value, double min, string text)
        {
            ValidationResult result = ValidationLogic.Min(value, min, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Max(this ModelStateDictionary modelStateDic, string key, double value, double max, string text)
        {
            ValidationResult result = ValidationLogic.Max(value, max, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void EqualOrMoreThan(this ModelStateDictionary modelStateDic, string key, double value, double min, string text)
        {
            ValidationResult result = ValidationLogic.EqualOrMorethan(value, min, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void MoreThan(this ModelStateDictionary modelStateDic, string key, double value, double min, string text)
        {
            ValidationResult result = ValidationLogic.Morethan(value, min, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void EqualOrLessThan(this ModelStateDictionary modelStateDic, string key, double value, double max, string text)
        {
            ValidationResult result = ValidationLogic.EqualOrLessthan(value, max, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void LessThan(this ModelStateDictionary modelStateDic, string key, double value, double max, string text)
        {
            ValidationResult result = ValidationLogic.Lessthan(value, max, text);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Enum(this ModelStateDictionary modelStateDic, string key, byte value, string text, Type enumType)
        {
            ValidationResult result = ValidationLogic.Enumerate(value, text, enumType);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }

        public static void Enum(this ModelStateDictionary modelStateDic, string key, string value, string text, Type enumType)
        {
            ValidationResult result = ValidationLogic.Enumerate(value, text, enumType);
            if (result.StatusCode == ValidationResultStatusCode.Fail)
            {
                modelStateDic.AddModelError(key, result.ErrorMsg);
            }
        }
    }
}
